<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'PHPUnit/Framework.php';
require_once 'autoload.php';

class TestAddressTranslator extends PHPUnit_Framework_TestCase {
	
	public function test_translate_addresses_to_db_keys_without_keyed_addresses() {
		$mock_mail = $this->getMock('InternalMail');
		$mock_mail->expects($this->once())
				  ->method("get_to_addresses")
				  ->will($this->returnValue('Jef'));
		$mock_mail->expects($this->once())
				  ->method("set_to_keys")
				  ->with(array(1));
		
		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->once())
					 ->method("get_keyed_values")
					 ->with($this->equalTo("user_id", "nick", "MEMBER"))
					 ->will($this->returnValue(array(1 => 'Jef', 2 => 'Klein peerke')));

		$translator = new AddressTranslator();
		$translator->set_toolbox($mock_toolbox);

		$translator->translate_addresses_to_db_keys(false, $mock_mail);
	}

	public function test_translate_addresses_to_db_keys_without_keyed_addresses_and_invalid_user_name() {
		try {
			$user = 'Jan';
			$mock_mail = $this->getMock('InternalMail');
			$mock_mail->expects($this->once())
					  ->method("get_to_addresses")
					  ->will($this->returnValue($user));
			$mock_mail->expects($this->never())
					  ->method("set_to_keys");
			
			$mock_toolbox = $this->getMock('Toolbox');
			$mock_toolbox->expects($this->once())
						 ->method("get_keyed_values")
						 ->with($this->equalTo("user_id", "nick", "MEMBER"))
						 ->will($this->returnValue(array(1 => 'Jef', 2 => 'Klein peerke')));

			$translator = new AddressTranslator();
			$translator->set_toolbox($mock_toolbox);

			$translator->translate_addresses_to_db_keys(false, $mock_mail);

			$this->fail("This line shouldn't be executed.");
		} catch (MailException $ex) {
			if($ex->getMessage() !== "'$user' is geen bekende gebruiker.")	{
				$this->fail("Correct exception, wrong message.");
			}
		}
	}

	public function test_translate_addresses_to_db_keys_without_keyed_addresses_and_user_is_system_user() {
		$user = 'Jef';

		$user_list = array(1 => 'Jef', 2 => 'Klein peerke');
		global $CONFIG;
		$CONFIG['system_user'] = 1;

		try {
			$mock_mail = $this->getMock('InternalMail');
			$mock_mail->expects($this->once())
					  ->method("get_to_addresses")
					  ->will($this->returnValue($user));
			$mock_mail->expects($this->never())
					  ->method("set_to_keys");
			
			$mock_toolbox = $this->getMock('Toolbox');
			$mock_toolbox->expects($this->once())
						 ->method("get_keyed_values")
						 ->with($this->equalTo("user_id", "nick", "MEMBER"))
						 ->will($this->returnValue($user_list));

			$translator = new AddressTranslator();
			$translator->set_toolbox($mock_toolbox);

			$translator->translate_addresses_to_db_keys(false, $mock_mail);

			$this->fail("This line shouldn't be executed.");
		} catch (MailException $ex) {
			if($ex->getMessage() !== "De systeemaccount ontvangt geen mail.") {
				$this->fail("Correct exception, wrong message.");
			}
		}
	}

	public function test_translate_addresses_to_db_keys_with_keyed_addresses() {
		$mock_mail = $this->getMock('InternalMail');
		$mock_mail->expects($this->once())
				  ->method("set_to_keys")
				  ->with(array(1, 2, 3));

		$mock_mail->to = "1,2 ;3";
		
		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->never())
					 ->method("get_keyed_values");

		$translator = new AddressTranslator();
		$translator->set_toolbox($mock_toolbox);

		$translator->translate_addresses_to_db_keys(true, $mock_mail);
	}

	public function test_translate_addresses_to_db_keys_with_keyed_addresses_and_cc_keys() {
		$mock_mail = $this->getMock('InternalMail');

		$mock_mail->expects($this->once())
				  ->method("set_to_keys")
				  ->with(array(1, 2, 3));

		$mock_mail->expects($this->once())
				  ->method("set_cc_keys")
				  ->with(array(4, 5, 6));

		$mock_mail->to = "1,2 ;3";
		$mock_mail->cc = "4 ;5, 6";
		
		$mock_toolbox = $this->getMock('Toolbox');
		$mock_toolbox->expects($this->never())
					 ->method("get_keyed_values");

		$translator = new AddressTranslator();
		$translator->set_toolbox($mock_toolbox);

		$translator->translate_addresses_to_db_keys(true, $mock_mail);
	}
}
